package com.linln.admin.SpringBoot.service.impl;

import com.linln.admin.SpringBoot.domain.Device;
import com.linln.admin.SpringBoot.domain.Lab;
import com.linln.admin.SpringBoot.repository.DeviceRepository;
import com.linln.admin.SpringBoot.repository.LabRepository;
import com.linln.admin.SpringBoot.service.DeviceService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private LabRepository labRepository;


    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Device getById(Long id) {
        return deviceRepository.findByIdd(id);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Device> getPageList(Example<Device> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        //魔改
        Page<Device> all = deviceRepository.findAll(example, page);
        for (Device device : all) {
            Optional<Lab> byId = labRepository.findById(device.getLab_id());
            if (byId.isPresent())
                device.setLabName(byId.get().getName());
        }
        return all;
    }

    /**
     * 保存数据
     * @param device 实体对象
     */
    @Override
    public Device save(Device device) {
        return deviceRepository.save(device);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return deviceRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}